<?php

namespace common\models;

use common\consts\Consts;
use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use common\models\File;

/**
 * FileSearch represents the model behind the search form about `common\models\File`.
 */
class FileSearch extends File
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['file_id', 'file_status', 'file_rows', 'file_result_code', 'file_rows_insert', 'file_rows_update', 'file_rows_skip', 'file_rows_error'], 'integer'],
            [['file_name', 'file_base_name', 'file_extension', 'file_path', 'file_fullname', 'file_uid', 'file_task_type', 'file_created_by', 'file_created_at', 'file_updated_by', 'file_updated_at'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = File::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination' => [
                'pageSize' => Consts::DEFAULT_PAGE_SIZE,
            ],
            'sort'=> ['defaultOrder' => ['file_created_at'=>SORT_DESC]],
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'file_id' => $this->file_id,
            'file_status' => $this->file_status,
            'file_rows' => $this->file_rows,
            'file_result_code' => $this->file_result_code,
            'file_rows_insert' => $this->file_rows_insert,
            'file_rows_update' => $this->file_rows_update,
            'file_rows_skip' => $this->file_rows_skip,
            'file_rows_error' => $this->file_rows_error,
            'file_created_at' => $this->file_created_at,
            'file_updated_at' => $this->file_updated_at,
        ]);

        $query->andFilterWhere(['like', 'file_name', $this->file_name])
            ->andFilterWhere(['like', 'file_base_name', $this->file_base_name])
            ->andFilterWhere(['like', 'file_extension', $this->file_extension])
            ->andFilterWhere(['like', 'file_path', $this->file_path])
            ->andFilterWhere(['like', 'file_fullname', $this->file_fullname])
            ->andFilterWhere(['like', 'file_uid', $this->file_uid])
            ->andFilterWhere(['like', 'file_task_type', $this->file_task_type])
            ->andFilterWhere(['like', 'file_created_by', $this->file_created_by])
            ->andFilterWhere(['like', 'file_updated_by', $this->file_updated_by]);

        return $dataProvider;
    }
}
